﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// Safe 安全区域，根据手机型号或者尺寸，设置UI弹窗的安全区域适配
/// </summary>
public class UISafeArea : MonoBehaviour
{

    [SerializeField]
    private EdgeRect edgeRect = new EdgeRect();

    [Tooltip("全匹配模式,匹配所有边")]
    public bool matchAllMode = true;

    [System.Serializable]
    public class EdgeRect
    {
        public bool Vertical = true;
        public float left;
        public float right;
        public float top;
        public float bottom;
    }

    private RectTransform rect;

    private void Awake()
    {
        rect = this.GetComponent<RectTransform>();
        this.UpdateContent();
    }


    public void UpdateContent()
    {
        this.edgeRect.left = this.edgeRect.right = this.edgeRect.top = this.edgeRect.bottom = 0;

        if (IsIPhoneX())
        {
            this.edgeRect.left = this.edgeRect.right = 0;
            this.edgeRect.top = -52;
            this.edgeRect.bottom = 34;
        }

        rect.offsetMin = new Vector2(this.edgeRect.left, this.edgeRect.bottom);
        rect.offsetMax = new Vector2(this.edgeRect.right, this.edgeRect.top);

    }

    public float GetScreenRatioVertical()
    {
        return (float)Screen.width / Screen.height;
    }


    public bool IsIPhoneX()
    {
        string modelStr = SystemInfo.deviceModel;

        //比18:9 的屏幕还狭窄的全面屏手机，那就处理一下适配
        return GetScreenRatioVertical() <= 0.5f || modelStr.Equals("iPhone10,3") || modelStr.Equals("iPhone10,6") || modelStr.Equals("iPhone11,8") || modelStr.Equals("iPhone11,2") || modelStr.Equals("iPhone11,6");

    }
}
